package com.zq.ht.common.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class FileRWUtil {
	private static File file = null;

    /**
     * 从本地文件读取文件为二进制流
     * 
     * @param infile
     * @return
     */
    public static FileInputStream getImageByte(String infile) {
        FileInputStream imageByte = null;
        file = new File(infile);
        try {
            imageByte = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return imageByte;
    }

    /**
     * 将流读出为文件
     * 
     * @param inputStream
     * @param path
     */
    public static void readBlob(InputStream inputStream, String path) {
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(path);
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = inputStream.read(buffer)) != -1) {
                fileOutputStream.write(buffer, 0, len);
            }
            inputStream.close();
            fileOutputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    
    
    /**
     * 将字节数组读出为文件
     * 
     * @param inputStream
     * @param path
     */
    public static void readBlob(byte[] data, String path) {
        try {
            FileOutputStream fileOutputStream = new FileOutputStream(path);
            fileOutputStream.write(data);
            fileOutputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    
    public static void main(String[] args) throws IOException {
    	String path = "d:/upload/avatar/h";
    	File f = new File(path);
    	if(!f.exists()){
    		f.mkdirs();
    	}
    	
    	File file = new File(f, "14454370981335.jpeg");
    	file.createNewFile();
    	FileOutputStream fileOutputStream = new FileOutputStream(path+"/14454370981335.jpeg");
	}

}
